
package com.loong.loong.hik.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loong.loong.common.core.util.R;
import com.loong.loong.hik.entity.CdcPeople;
import com.loong.loong.hik.mapper.CdcPeopleMapper;
import com.loong.loong.hik.property.urls.face.HikFaceUrl;
import com.loong.loong.hik.service.CdcPeopleService;
import com.loong.loong.hik.service.HikCdcVService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 疾控中心人员表
 *
 * @author cs
 * @date 2023-04-13 10:35:46
 */
@Service
public class CdcPeopleServiceImpl extends ServiceImpl<CdcPeopleMapper, CdcPeople> implements CdcPeopleService {
	@Resource
	private HikCdcVService hikCdcVService;

	@Override
	public R add(CdcPeople cdcPeople) {
		//掉接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			try {
				//API 调用的结果存储在FaceGroupResult对象中
				R r = hikCdcVService.updatePeople(cdcPeople);
				if (r==null||r.getCode()!=0){
					return R.failed("接口调用失败，原因："+r.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		CdcPeople cdcPeople1 = new CdcPeople();

		cdcPeople1.setPin(cdcPeople.getPin());
		long count = count(Wrappers.query(cdcPeople1));

		if (count>0){
			return R.failed("已存在相同的人员编号："+cdcPeople1.getPin());
		}else{
			return R.ok(save(cdcPeople));
		}
	}

	@Override
	public R del(Integer id) {
		CdcPeople byId = getById(id);
		if (byId==null){
			R.failed("删除失败，根据id查询为空");
		}
		if (StringUtils.isEmpty(byId.getPin())){
			R.failed("删除失败，人员编号为空");
		}
		//掉接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			try {
				//API 调用的结果存储在FaceGroupResult对象中
				R r = hikCdcVService.deletePeople(byId);
				if (r==null||r.getCode()!=0){
					return R.failed("接口调用失败，原因："+r.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return R.ok(removeById(id));
//		return R.ok();
	}

	@Override
	public R update(CdcPeople cdcPeople) {

		//掉接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			try {
				//API 调用的结果存储在FaceGroupResult对象中
				R r = hikCdcVService.updatePeople(cdcPeople);
				if (r==null||r.getCode()!=0){
					return R.failed("接口调用失败，原因："+r.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return R.ok(updateById(cdcPeople));
	}
}
